/*
 * @Author: myw
 * @Email: 516378746@qq.com
 * @Date: 2020-03-02 17:04:07
 * @Descripttion: 移库出库
 */
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MovewareOutwareService } from './moveware-outware.service';
import { DatePipe } from '@angular/common';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';

@Component({
    selector: 'app-moveware-outware',
    templateUrl: './moveware-outware.component.html',
    styleUrls: ['./moveware-outware.component.scss']
})
export class MovewareOutwareComponent implements OnInit {

    selectedValue = '';
    listData = [];
    totalSize: number = 0;
    pageIndex: number = 1;
    pagesize: number = 10;
    rangeDate: string[] = [];
    sourceHouseData = [];
    chooseWareTypeList = []

    constructor(
        private outwareService: MovewareOutwareService,
        private date: DatePipe,
        private modal: NzModalService,
        private nzmessage: NzMessageService
    ) { }

    async ngOnInit() {
        this.getSourceHouseList();
        await this.getChooseWareType();
        this.getList();
    }
    onSearchHouseChange(id) {
        this.pageIndex = 1;
        this.getList();
    }
    onDateChange(date) {
        this.rangeDate[0] = this.date.transform(date[0], 'yyyy-MM-dd 00:00:00')
        this.rangeDate[1] = this.date.transform(date[1], 'yyyy-MM-dd 23:59:59')
        this.getList();
    }
    getSourceHouseList() {
        this.outwareService.apiSourceHouse().subscribe(res => {
            this.sourceHouseData = res;
        })
    }
    getList() {
        let _newData = {}
        let _dataJson = {
            page: this.pageIndex,
            size: this.pagesize,
            fromStorehouse: this.selectedValue,
            sort: 'createDate,desc'
        }
        if (!this.rangeDate[0]) {
            _newData = {
                ..._dataJson
            }
        } else {
            _newData = {
                ..._dataJson,
                createDate: this.rangeDate
            }
        }
        this.outwareService.apiList(_newData).subscribe(res => {
            this.listData = res.content;
            this.listData.forEach(item => {
                this.chooseWareTypeList.forEach($item => {
                    if ($item.value === item.pickingType) {
                        item.chooseType = $item.label
                    }
                })
            })
            this.totalSize = res.totalElements;
        })
    }
    getChooseWareType() {
        return new Promise((resolve, reject) => {
            this.outwareService.apiChooseTypeList("picking_type").subscribe(res => {
                this.chooseWareTypeList = res;
                resolve();
            })
        })
    }
    clickPage(pageIndex) {
        this.pageIndex = pageIndex;
        this.getList();
    }

    submitFn(data) {
        let _r = this.filterResNum(data.id);
        if (!_r.resAmount) {
            this.nzmessage.create('error', '请输入移库量！')
            return false
        } else if (Number(_r.resAmount) > Number(data.fromStorehouseAmount)) {
            this.nzmessage.create('error', '实际移库量不可超过原库房可用量！')
            return false
        } else {
            this.modal.confirm({
                nzTitle: "提示",
                nzContent: "确定提交数据？",
                nzOnOk: () => {
                    let _dataJson = {
                        id: data.id,
                        resAmount: _r.resAmount,
                        status: 3
                    }
                    this.outwareService.apiStatus(_dataJson).subscribe(res => {
                        this.nzmessage.success("提交成功");
                        this.pageIndex = 1;
                        this.getList();
                    })
                }
            })
        }
    }
    returnFn(data) {
        this.modal.confirm({
            nzTitle: "提示",
            nzContent: "确定退回吗？",
            nzOnOk: () => {
                let _r = this.filterResNum(data.id);
                let _dataJson = {
                    id: data.id,
                    resAmount: _r.resAmount,
                    status: 4
                }
                this.outwareService.apiStatus(_dataJson).subscribe(res => {
                    this.nzmessage.success("提交成功");
                    this.pageIndex = 1;
                    this.getList();
                })
            }
        })
    }
    filterResNum(id) {
        return this.listData.filter(item => {
            return item.id === id
        })[0]
    }



}
